package org.lql.algo.codecrush.hot100.hash;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: liangqinglong
 * @date: 2025-09-16 18:03
 * @description: 49. 字母异位词分组 <a href="https://leetcode.cn/problems/group-anagrams/description/?envType=study-plan-v2&envId=top-100-liked">...</a>
 **/
public class GroupAnagrams {

	public List<List<String>> groupAnagrams(String[] strs) {
		Map<String, List<String>> groups = new HashMap<>();
		for (String str : strs) {
			char[] charArray = str.toCharArray();
			Arrays.sort(charArray);
			String key = new String(charArray);
			groups.computeIfAbsent(key, k -> new ArrayList<>()).add(str);
		}
		return new ArrayList<>(groups.values());
	}

	public static void main(String[] args) {
		GroupAnagrams groupAnagrams = new GroupAnagrams();
		System.out.println(groupAnagrams.groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}));
		System.out.println(groupAnagrams.groupAnagrams(new String[]{""}));
		System.out.println(groupAnagrams.groupAnagrams(new String[]{"a"}));
	}
}
